/*
 * @Author: wurangkun wurangkun@qq.com
 * @Date: 2023-07-07 10:33:04
 * @LastEditors: wurangkun wurangkun@qq.com
 * @LastEditTime: 2023-07-07 12:17:09
 * @FilePath: \reset_sever\module\stop.js
 * @Description: 停止自动关机
 */
const { exec } = require('child_process');
const notifier = require('node-notifier');
const fs = require('fs')
const path = require('path');
const filePath = path.join(__dirname, './data/pid.text');
const timePath = path.join(__dirname, './data/time.text');
let pid = +fs.readFileSync(filePath).toString('utf-8')
const readline = require('readline');
const { password } = require('../config');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

const stopShutdown = () => {
  if (!pid) {
    notifier.notify({
      title: '提示',
      message: `没有可以清除的自动关机程序`,
      function(error, response) {
        console.log(response, error);
      }
    });
    return
  }
  rl.question('password：', (answer) => {
    console.log(`您输入的内容是：${answer}`);
    if (answer === password) {
      exec(`taskkill /PID ${pid} /F`, (error, stdout, stderr) => {
        if (error) {
          console.error(`执行命令时出错：${error.message}`);
          return;
        }
        fs.writeFileSync(filePath, '')
        fs.writeFileSync(timePath, '0')
        rl.close();
        notifier.notify({
          title: '提示',
          message: `自动关机程序已被清除`,
          function(error, response) {
            console.log(response, error);
          }
        });
        // 命令成功执行
        console.log(stdout);
      });
    } else {
      console.log('密码错误,自动关机程序清除失败');
      notifier.notify({
        title: '提示',
        message: `密码错误,自动关机程序清除失败`,
        function(error, response) {
          console.log(response, error);
        }
      });
      stopShutdown()
    }
  });

}

stopShutdown()